#!/bin/bash

source ../scripts/common.sh

export PATH=$PATH:/usr/sbin
export USER_GROUP=www
export USER_NAME=nginx


export TENGINE_VERSION=2.3.3
export TENGINE_MD5SUM=01651b1342c406b933490dd8f2962b36
export JEMALLOC_VESION=5.2.1

#export LUA_VERSION=5.1
#export LUA_NGINX_VESION=0.10.17
#export LUA_WEBSOCKET_VERSION=0.07
#export LUAJIT_VERSION=2.1-20200102
#export LUA_RESTY_MYSQL_VERSION=0.22
#export LUA_RESTY_CORE_VERSION=0.1.19
#export LUA_RESTY_LRUCACHE_VERSION=0.10
#export LUA_RESTY_MEMCACHED_VERSION=0.15
#export LUA_RESTY_REDIS_VERSION=0.28


egrep "^${USER_GROUP}:" /etc/group >& /dev/null  
if [ $? -ne 0 ]; then
	groupadd $USER_GROUP
fi

egrep "^${USER_GROUP}.*[:,]${USER_NAME}$" /etc/group >& /dev/null  
if [ $? -ne 0 ]; then
	useradd -G $USER_GROUP -M -s /usr/sbin/nologin $USER_NAME
fi	

if [ $OS_TYPE = "Centos" ]; then
yum install -y git zlib1g-devel libgd-devel libcurl4-openssl-devel libpcre3-devel gcc cmake perl-FindBin perl-JSON
elif [ $OS_TYPE = "Debian" ]; then
apt install -y git zlib1g-devel libgd-devel libcurl4-openssl-devel libpcre3-devel gcc cmake 
else
	echo -e "\033[31m[ERROR]\033[0m 目前只支持 Debian 和 Centos"
	exit 0;
fi


#下载资源包
download http://tengine.taobao.org/download/tengine-$TENGINE_VERSION.tar.gz tengine-$TENGINE_VERSION.tar.gz $TENGINE_MD5SUM;
download https://github.com/jemalloc/jemalloc/releases/download/$JEMALLOC_VESION/jemalloc-$JEMALLOC_VESION.tar.bz2 jemalloc-$JEMALLOC_VESION.tar.bz2 '3d41fbf006e6ebffd489bdb304d009ae';
download https://www.openssl.org/source/openssl-$OPENSSL_VERSION.tar.gz openssl-$OPENSSL_VERSION.tar.gz $OPENSSL_MD5SUM;
#download https://github.com/openresty/lua-nginx-module/archive/v$LUA_NGINX_VESION.tar.gz lua-nginx-module-$LUA_NGINX_VESION.tar.gz '808a2bb2514f52a94fabb275dba14cb6';
#download https://github.com/openresty/lua-resty-websocket/archive/v$LUA_WEBSOCKET_VERSION.tar.gz lua-resty-websocket-$LUA_WEBSOCKET_VERSION.tar.gz 'd5e5546743feac8756b27d593ceab32e';
#download https://github.com/openresty/lua-resty-mysql/archive/v$LUA_RESTY_MYSQL_VERSION.tar.gz lua-resty-mysql-$LUA_RESTY_MYSQL_VERSION.tar.gz '019ca28f582bc853f9a827756eb53026'
#download https://github.com/openresty/luajit2/archive/v$LUAJIT_VERSION.tar.gz luajit2-$LUAJIT_VERSION.tar.gz '94ef094519fbbf36fc52c3f8c1f6d7e9'
#download https://github.com/openresty/lua-resty-core/archive/v${LUA_RESTY_CORE_VERSION}.tar.gz lua-resty-core-${LUA_RESTY_CORE_VERSION}.tar.gz 'b697a1b28f8a99471053733d9e54c2c6'
#download https://github.com/openresty/lua-resty-lrucache/archive/v${LUA_RESTY_LRUCACHE_VERSION}.tar.gz lua-resty-lrucache-${LUA_RESTY_LRUCACHE_VERSION}.tar.gz 'a7d54a672ef2a575dc2e9d46ec1b292e'
#download https://github.com/openresty/lua-resty-redis/archive/v${LUA_RESTY_REDIS_VERSION}.tar.gz lua-resty-redis-${LUA_RESTY_REDIS_VERSION}.tar.gz 'eee063747e05d356f12d2208fb4d3488'
#download https://github.com/openresty/lua-resty-memcached/archive/v${LUA_RESTY_MEMCACHED_VERSION}.tar.gz lua-resty-memcached-${LUA_RESTY_MEMCACHED_VERSION}.tar.gz '80022edac3d927fef6eeac0689a1dd30'

if [ ! -d tengine-$TENGINE_VERSION ]; then tar -xvf tengine-$TENGINE_VERSION.tar.gz; fi
if [ ! -d jemalloc-$JEMALLOC_VESION ]; then tar -xvjf jemalloc-$JEMALLOC_VESION.tar.bz2; fi
if [ ! -d openssl-$OPENSSL_VERSION ]; then tar -xvf openssl-$OPENSSL_VERSION.tar.gz; fi
#if [ ! -d lua-nginx-module-$LUA_NGINX_VESION ]; then tar -xvf lua-nginx-module-$LUA_NGINX_VESION.tar.gz; fi
#if [ ! -d luajit2-$LUAJIT_VERSION ]; then tar -xvf luajit2-$LUAJIT_VERSION.tar.gz; fi


#rm -rf lua-resty-core-$LUA_RESTY_CORE_VERSION;
#rm -rf lua-resty-mysql-$LUA_RESTY_MYSQL_VERSION;
#rm -rf lua-resty-websocket-$LUA_WEBSOCKET_VERSION;
#rm -rf lua-resty-lrucache-$LUA_RESTY_LRUCACHE_VERSION;


#if [ ! -d lua-resty-core-$LUA_RESTY_CORE_VERSION ]; then
#	tar -xvf lua-resty-core-$LUA_RESTY_CORE_VERSION.tar.gz;
#	sed -i 's#PREFIX ?=          /usr/local#PREFIX ?=          /usr/local/share/#' lua-resty-core-$LUA_RESTY_CORE_VERSION/Makefile
#	sed -i 's#LUA_LIB_DIR ?=     $(PREFIX)/lib/lua/#LUA_LIB_DIR ?=     $(PREFIX)/lua/#' lua-resty-core-$LUA_RESTY_CORE_VERSION/Makefile
#fi
#
#if [ ! -d lua-resty-mysql-$LUA_RESTY_MYSQL_VERSION ];
#	then tar -xvf lua-resty-mysql-$LUA_RESTY_MYSQL_VERSION.tar.gz;
#	sed -i 's#PREFIX ?=          /usr/local#PREFIX ?=          /usr/local/share#' lua-resty-mysql-$LUA_RESTY_MYSQL_VERSION/Makefile
#	sed -i 's#LUA_LIB_DIR ?=     $(PREFIX)/lib/lua/#LUA_LIB_DIR ?=     $(PREFIX)/lua/#' lua-resty-mysql-$LUA_RESTY_MYSQL_VERSION/Makefile
#fi
#
#if [ ! -d lua-resty-websocket-$LUA_WEBSOCKET_VERSION ]; then 
#	tar -xvf lua-resty-websocket-$LUA_WEBSOCKET_VERSION.tar.gz;
#	sed -i 's#PREFIX ?=          /usr/local#PREFIX ?=          /usr/local/share#' lua-resty-websocket-$LUA_WEBSOCKET_VERSION/Makefile
#	sed -i 's#LUA_LIB_DIR ?=     $(PREFIX)/lib/lua/#LUA_LIB_DIR ?=     $(PREFIX)/lua/#' lua-resty-websocket-$LUA_WEBSOCKET_VERSION/Makefile
#fi


#if [ ! -d lua-resty-lrucache-$LUA_RESTY_LRUCACHE_VERSION ]; then
#	tar -xvf lua-resty-lrucache-$LUA_RESTY_LRUCACHE_VERSION.tar.gz;
#	sed -i 's#PREFIX ?=          /usr/local#PREFIX ?=          /usr/local/share#' lua-resty-lrucache-$LUA_RESTY_LRUCACHE_VERSION/Makefile
#	sed -i 's#LUA_LIB_DIR ?=     $(PREFIX)/lib/lua/#LUA_LIB_DIR ?=     $(PREFIX)/lua/#' lua-resty-lrucache-$LUA_RESTY_LRUCACHE_VERSION/Makefile
#fi
#
#if [ ! -d lua-resty-redis-$LUA_RESTY_REDIS_VERSION ]; then
#	tar -xvf lua-resty-redis-$LUA_RESTY_REDIS_VERSION.tar.gz;
#	sed -i 's#PREFIX ?=          /usr/local#PREFIX ?=          /usr/local/share#' lua-resty-redis-$LUA_RESTY_REDIS_VERSION/Makefile
#	sed -i 's#LUA_LIB_DIR ?=     $(PREFIX)/lib/lua/#LUA_LIB_DIR ?=     $(PREFIX)/lua/#' lua-resty-redis-$LUA_RESTY_REDIS_VERSION/Makefile
#fi
#
#if [ ! -d lua-resty-memcached-$LUA_RESTY_MEMCACHED_VERSION ]; then
#	tar -xvf lua-resty-memcached-$LUA_RESTY_MEMCACHED_VERSION.tar.gz;
#	sed -i 's#PREFIX ?=          /usr/local#PREFIX ?=          /usr/local/share#' lua-resty-memcached-$LUA_RESTY_MEMCACHED_VERSION/Makefile
#	sed -i 's#LUA_LIB_DIR ?=     $(PREFIX)/lib/lua/#LUA_LIB_DIR ?=     $(PREFIX)/lua/#' lua-resty-memcached-$LUA_RESTY_MEMCACHED_VERSION/Makefile
#fi

#if [ -d lua-resty-core-${LUA_RESTY_CORE_VERSION} ]; then
#	pushd lua-resty-core-${LUA_RESTY_CORE_VERSION}
#	make install
#	popd
#fi

#if [ -d lua-resty-lrucache-$LUA_RESTY_LRUCACHE_VERSION ]; then
#	pushd lua-resty-lrucache-$LUA_RESTY_LRUCACHE_VERSION
#	make install
#	popd
#fi

#if [ -d lua-resty-mysql-$LUA_RESTY_MYSQL_VERSION ]; then
#	pushd lua-resty-mysql-$LUA_RESTY_MYSQL_VERSION
#	make install
#	popd
#fi

#if [ -d lua-resty-redis-$LUA_RESTY_REDIS_VERSION ]; then
#	pushd lua-resty-redis-$LUA_RESTY_REDIS_VERSION
#	make install
#	popd
#fi

#if [ -d lua-resty-memcached-$LUA_RESTY_MEMCACHED_VERSION ]; then
#	pushd lua-resty-memcached-$LUA_RESTY_MEMCACHED_VERSION
#	make install
#	popd
#fi


# 克隆缩略图生成模块
if [ ! -d ngx_image_thumb ]; then
	git clone https://gitee.com/bywayboy/ngx_image_thumb.git;
else
	pushd ngx_image_thumb
	git pull
	popd
fi


#编译LUAJIT
#pushd luajit2-$LUAJIT_VERSION
#make && make install
#popd

#LUAJIT符号连接
#if [ ! -f /usr/lib/libluajit-5.1.so.2 ]; then
#	ln -s /usr/local/lib/libluajit-5.1.so.2 /usr/lib/libluajit-5.1.so.2
#fi

#编译tengine
pushd tengine-$TENGINE_VERSION
export LUAJIT_INC=/usr/local/include/luajit-2.1
./configure --user=$USER_NAME --group=$USER_GROUP --with-jemalloc=../jemalloc-$JEMALLOC_VESION --with-openssl=../openssl-$OPENSSL_VERSION \
--add-module=modules/ngx_http_concat_module \
--add-module=../ngx_image_thumb \
--add-module=modules/ngx_http_reqstat_module \
--with-http_realip_module \
--with-http_v2_module \
--with-threads \
--with-http_addition_module \
--with-cc-opt="-D NGX_HAVE_GNU_CRYPT_R=0" 

make -j${THREAD_NUM} && make install
popd


cat  <<EOF 	> /lib/systemd/system/nginx.service
[Unit]
Description=The nginx HTTP and reverse proxy server
After=network.target php-fpm.service

[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStartPre=/usr/local/nginx/sbin/nginx -t
ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true
 
[Install]
WantedBy=multi-user.target
EOF

#修改默认配置
sed -i 's#\#user  nobody;#user  nginx www;#' /usr/local/nginx/conf/nginx.conf
sed -i 's#worker_processes  1;#worker_processes 4;#' /usr/local/nginx/conf/nginx.conf

# 启动服务
systemctl enable nginx.service
systemctl start nginx.service


# -with-http_ssl_module
